<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <script src="./assets/jquery-3.2.1.min.js"></script>
  <script src="../build/g6.js"></script>
  <script src="../build/plugins.js"></script>
  <title>云栖 demo 1</title>
</head>

<body>
  <div id="mountNode"></div>
  <script>
    $.getJSON('./assets/data/usa-president.json', table => {
      // table.forEach(sub=>{
      //   sub.age_decade = parseInt(sub.age_decade/10)*10;
      // });
      // console.log(JSON.stringify(table, null, '  '));
      
      // table combine field value view
      setTimeout(()=>{
        const combineFieldViewCfg = {
          combine({ field, value }) {
            return field + value;
          }
        };

        // table full view cfg
        const fullViewCfg = {
          combine({ field, value,colIndex,rowIndex }) {
            return field + value + colIndex + rowIndex;
          }
        };

        const sankeyPlugin = new G6.Plugins['template.tableSankey']({
          table,
          fields: ['president', 'birth_place', 'age_decade', 'party', 'sgin'],
          onBeforeRender(graph) {
            graph.node({
              color(model) {
                const colors = ['#FD8C3D', '#D83F43', '#F7BED6', '#E487C7', '#46A848', '#D83F43', '#3B85BA', '#48335B', '#B7CDE9'];
                return colors[model.rowIndex%10];
              },
              style: {
                stroke: '#616161'
              }
            });
            graph.edge({
              style: {
                stroke: 'rgb(0, 0, 0)',
                strokeOpacity: 0.2
              }
            });
          },
          ...fullViewCfg
        })
        const graph = new G6.Graph({
          container: 'mountNode',
          height: window.innerHeight, // 画布高
          fitView: 'cc',
          animate: true,
          plugins: [sankeyPlugin]
        });
        setTimeout(()=>{
          sankeyPlugin.change(combineFieldViewCfg);
        }, 2000);
        setTimeout(()=>{
          sankeyPlugin.change({
            fields: ['president', 'birth_place', 'party', 'sgin'],
          });
        }, 4000);
        setTimeout(()=>{
          sankeyPlugin.change({
            fields: ['president', 'birth_place', 'party'],
          });
        }, 6000);
        setTimeout(()=>{
          sankeyPlugin.change({
            fields: ['president', 'birth_place', 'party', 'age_decade'],
          });
        }, 8000);
      }, 0);

    });
  </script>
</body>
</html>

